package juliusapp.sys.main;

import juliusapp.controller.UserController;
import juliusapp.model.UserVO;
import juliusapp.util.dialog.OkDialog;
import juliusapp.util.validator.EmailValidator;
import juliusapp.view.user.ActivityUserRegister;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityPrincipal extends Activity {

	private TextView linkToRegister,email,password;
	private Button btnLogin;
	
	private UserVO vo;
	private UserController userController;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);

		linkToRegister = (TextView) findViewById(R.id.link_to_register);		
		email= (TextView) findViewById(R.id.edtemail);
		password = (TextView) findViewById(R.id.edtpassword);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLogin.requestFocus();
		userController = new UserController(getBaseContext());

		linkToRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					linkToRegisterAction();
				} catch (Exception e) {
					goToDialogs("Falha ao chamar a tela.\n" + e.getMessage());
				}
			}
		});

		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					btnLoginAction();
				} catch (Exception e) {
					goToDialogs(e.getMessage());
				}
			}
		});

	}
	
	private boolean canLogin() throws Exception{
		try {
			
			if(email.getText().toString().isEmpty()){				
				email.setError(getResources().getText(R.string.requiredEmail));
				email.requestFocus();
				return false;
			}
			else if(EmailValidator.isInvalid(email.getText().toString())){
				email.setError(getResources().getText(R.string.invalidEmail));
				email.requestFocus();
				return false;
			}
			else if(password.getText().toString().isEmpty()){
				password.setError(getResources().getText(R.string.requiredPassword));				
				password.requestFocus();
				return false;
			}
			
			return true;
		} catch (Exception e) {
			throw new Exception("validLogin()|"+e.getMessage());
		}
		
	}

	/**
	 * Ação do botão login
	 * @throws Exception
	 */
	protected void btnLoginAction() throws Exception{
		try {
		if(canLogin()){
			Toast t;
			vo = new UserVO();			
			vo.setEmail(email.getText().toString());
			vo.setPassword(password.getText().toString());
			if(userController.Login(vo)){					
				t = Toast.makeText(getBaseContext(),"Sucesso",Toast.LENGTH_SHORT);
				t.show();
			}else
			{
				t = Toast.makeText(getBaseContext(), getResources().getString(R.string.loginFail),Toast.LENGTH_SHORT);
				t.show();
			}
		}
		} catch (Exception e) {
		throw new Exception("btnAction()|"+e.getMessage());
		}
	}
	
	
	

	protected void linkToRegisterAction() throws Exception {
		try {

			Intent it = new Intent(this, ActivityUserRegister.class);
			startActivity(it);
		} catch (Exception e) {
			e.getMessage();
		}
	}

	private void goToDialogs(String msg) {
		OkDialog.show(msg, this);
	}

	void btnLogin() {
		Log.i("MINHA INFORMA��O", "PASSOU AQUI");
	}

}
